// pages/login/index.js
import {
  wechat,
  user,
  staff
} from "../../api/apiData"
import Toast from '@vant/weapp/toast/toast';


Page({

  /**
   * 页面的初始数据
   */
  data: {
    code: "",
    username:"",
    password:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    wx.login({})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  setusername(e){
    var val = e.detail.value
    this.setData({
      username:val
    })
  },
  setpassword(e){
    
    var val = e.detail.value
    this.setData({
      password:val
    })
  },
  Login(){
    let {username,password} = this.data
    Toast.loading()
    staff.login({username,password},res=>{
      let {code,msg,data} = res
      if(code==1){
        console.log(res)
        wx.setStorage({key:"token",data:data.token})
        wx.setStorage({key:"type",data:'Staff'})
        // wx.switchTab({
        //     url: '/pages/index/index',
		// })
		wx.navigateBack({
			delta: 0,
		  })
        Toast.clear()
      }else{
        Toast(msg)
      }

    })
  },
  VisiLogin() {
    let _this = this
    
    let iv, encryptedData
    
    wx.getUserProfile({
      desc: "个人信息",
      success: function (res) {
        
        iv = res.iv
        encryptedData = res.encryptedData
        Toast.loading()
        wx.login({
          success(res) {
            
            wechat.userLogin({code:res.code,iv,encryptedData}, res => {

              let {code,msg,data} = res
              console.log(res)
              Toast.clear()
              if(code==0){
                Toast(msg)
              }else{
                wx.setStorage({key:"token",data:data.token})
                wx.setStorage({key:"type",data:'User'})
                // wx.switchTab({
                //   url: '/pages/index/index',
				// })
				wx.navigateBack({
				  delta: 0,
				})
              }
              
            })
          }
        })

      },
      fail(err) {
        
        
      }
    })


  },
  WxLogin(){
    wx.login({
      success(res) {
        let {code} = res
        wechat.staffLogin({code},res=>{
          let {msg,data} =res
          if(res.code==1){
            console.log(res)
            wx.setStorage({key:"token",data:data.token})
            wx.setStorage({key:"type",data:'Staff'})
            wx.switchTab({
                url: '/pages/index/index',
            })
            Toast.clear()
          }else{
            Toast(msg)
          }
        })

      }
    })  
    
  }
})